//
//  SelectImageCell.m
//  BXlive
//
//  Created by bxlive on 2017/5/8.
//  Copyright © 2017年 cat. All rights reserved.
//

#import "SelectImageCell.h"
#import "ImageCollectionCell.h"
#import "UploadImageHandle.h"
#import <SDAutoLayout/SDAutoLayout.h>
#import <Masonry/Masonry.h>
#import <YYCategories/YYCategories.h>


@interface SelectImageCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@end

@implementation SelectImageCell


- (NSMutableArray *)imageArrayM
{
    if (!_imageArrayM) {
        _imageArrayM = [NSMutableArray array];
    }
    
    return _imageArrayM;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        self.maxImageNumber = 1;
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor =sl_textColors;
        self.titleLabel.font = SLBFont(16);
        [self addSubview:self.titleLabel];
        self.numberLabel = [[UILabel alloc] init];
        self.numberLabel.font = SLPFFont(14);
        self.numberLabel.textColor = sl_textSubColors;
        [self addSubview:self.numberLabel];
        self.numberLabel.text = [NSString stringWithFormat:@"(0/%ld)",(long)self.maxImageNumber];
        self.titleLabel.sd_layout
        .leftSpaceToView(self,__ScaleWidth(12)).topSpaceToView(self,__ScaleWidth(10)).widthIs(50).heightIs(20);
        
        self.numberLabel.sd_layout
        .leftSpaceToView(self.titleLabel,0).topEqualToView(self.titleLabel).heightRatioToView(self.titleLabel,1);
        
        
        self.minSelectLabel = [UILabel initWithFrame:CGRectZero size:14 color:UIColorHex(999999) alignment:NSTextAlignmentRight lines:1]; 
        [self addSubview:self.minSelectLabel];
        self.minSelectLabel.text = @"";
        self.minSelectLabel.sd_layout.rightSpaceToView(self,16).centerYEqualToView(self.numberLabel).widthIs(100).heightRatioToView(self.numberLabel,1);
        
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(__ScaleWidth(105), __ScaleWidth(105));
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [self addSubview:self.collectionView];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.collectionView.backgroundColor = [UIColor clearColor];
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        WS(weakSelf);
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.titleLabel);
            make.right.equalTo(weakSelf).offset(__ScaleWidth(16));
            make.bottom.mas_equalTo(0);
            make.height.mas_equalTo(__ScaleWidth(110));
        }];
//        self.collectionView.sd_layout.leftEqualToView(self.titleLabel).rightSpaceToView(self,16).bottomSpaceToView(self,16).heightIs(48);
        [self.collectionView registerClass:[ImageCollectionCell class] forCellWithReuseIdentifier:@"ImageCollectionCell"];
    }
    return self;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self bringSubviewToFront:self.collectionView];
    [self bringSubviewToFront:self.titleLabel];
    [self bringSubviewToFront:self.numberLabel];
    [self bringSubviewToFront:self.minSelectLabel];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    self.numberLabel.text = [NSString stringWithFormat:@"(%ld/%ld)",self.imageArrayM.count,(long)self.maxImageNumber];
    
    if (self.imageArrayM.count<self.maxImageNumber) {
      return self.imageArrayM.count+1;
    }

    return self.imageArrayM.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCollectionCell" forIndexPath:indexPath];
    if (indexPath.item<self.imageArrayM.count) {
        
        if ([self.imageArrayM[indexPath.item] isKindOfClass:[NSDictionary class]]) {
            
        }else{
            cell.imageView.image = self.imageArrayM[indexPath.item];
        }
        
        cell.deleteBtn.hidden = NO;
        [cell deleteImage:^{
            
            if (indexPath.item<self.imageArrayM.count&&self.imageArrayM.count>0) {
                [self.imageArrayM removeObjectAtIndex:indexPath.item];
            }
            
            [self.collectionView reloadData];
            if (self.selectImageBlock) {
                self.selectImageBlock(self.imageArrayM);
            }
        }];

    }else{
        cell.imageView.image = [UIImage imageNamed:@"tjtp"];
        cell.deleteBtn.hidden = YES;
    }
    return cell;
}


#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.superview endEditing:YES];
    
    
   NSInteger selectNumber = self.maxImageNumber-self.imageArrayM.count;
    
    if (selectNumber<=0) {
        [BGProgressHUD showInfoWithMessage:[NSString stringWithFormat:NSLocalizedString( @"最多可以选择%ld张!", nil),(long)self.maxImageNumber]];
        return;
    }
    
    
    
    [[UploadImageHandle sharedUploadImageHandle] uploadImageWithMaxSelectNumber:selectNumber editTpye:1 VC:self.viewController Sucess:^(NSArray *ImageArray) {
        
        [self.imageArrayM addObjectsFromArray:ImageArray];
        [self.collectionView reloadData];
        
        if (self.selectImageBlock) {
            self.selectImageBlock(self.imageArrayM);
        }
        
    } Failure:^(NSString *errorMsg) {
        
    }];

}


- (void)returnSelectImages:(SelectImageBlock)selectImageBlock
{
    _selectImageBlock = selectImageBlock;
}

- (void)setMaxImageNumber:(NSInteger)maxImageNumber
{
    _maxImageNumber = maxImageNumber;
    self.numberLabel.text = [NSString stringWithFormat:@"(0/%ld)",(long)maxImageNumber];
    [self.collectionView reloadData];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
